# @Time    : 2021/1/26 14:42
# @Author  : sxh
# @Email   : 116109899@qq.com
# @File    : angle.py
# 角的换算
"""
角的度量单位通常有两种，一种是角度制degrees，另一种就是弧度制rads。
角度制，就是用角的大小来度量角的大小的方法。
在角度制中，我们把周角的1/360看作1度，那么，半周就是180度，一周就是360度。
角度制是60进制，1°=60′，1′=60″，1′=(1/60)°，1″=(1/60)′。

"""

from math import pi


def rads2degrees(rads: float):
    """
    弧度换算成角度
    :param rads:
    :return:
    """
    degrees = (rads * 180.0) / pi
    return degrees


def degrees2rads(degrees: float):
    """
    角度换算成弧度
    :param degrees:
    :return:
    """
    rads = (degrees * pi) / 180.0
    return rads


def decdeg2dms(dd: float):
    """
    十进制角度转换成度分秒
    convert Decimal Degrees to Degrees Minutes Seconds
    :param dd:
    :return:
    """
    is_positive = dd >= 0
    dd = abs(dd)
    minutes,seconds = divmod(dd*3600,60)
    degrees,minutes = divmod(minutes,60)
    degrees = degrees if is_positive else -degrees
    return (degrees,minutes,seconds)


def dms2decdeg(degrees, minutes, seconds):
    """
    度分秒转换成十进制角度
    :param degrees:
    :param minutes:
    :param seconds:
    :param direction:
    :return:
    """
    is_positive = degrees >= 0
    degrees = abs(degrees)
    decdeg = float(degrees) + float(minutes)/60 + float(seconds)/(60*60)
    decdeg = decdeg if is_positive else -decdeg
    return decdeg


def angle(data:dict):
    """
    计算度数，输入一个字典，返回一个字典
    :param data:字典，
    :return res:字典
    """
    if "rads" in data:
        rads = data["rads"]
        decdeg = rads2degrees(rads)
        (degrees, minutes, seconds) = decdeg2dms(decdeg)
    elif "decdeg" in data:
        decdeg = data["decdeg"]
        rads = degrees2rads(decdeg)
        (degrees, minutes, seconds) = decdeg2dms(decdeg)
    elif "dms" in data:
        dms =  data["dms"]
        degrees = dms["degrees"]
        minutes = dms["minutes"]
        seconds = dms["seconds"]
        decdeg = dms2decdeg(degrees,minutes,seconds)
        rads = degrees2rads(decdeg)
    res = {
        "rads": rads,
        "decdeg": decdeg,
        "dms": {
            "degrees": degrees,
            "minutes": minutes,
            "seconds": seconds,
        }
    }
    return res

def test():
    """
    测试函数
    :return:

    """
    # res = {
    #     "rads": 0,
    #     "decdeg": 0,
    #     "dms": {
    #         "degrees": 0,
    #         "minutes": 0,
    #         "seconds": 0,
    #     }
    # }
    data1 = {
        "rads": -1.2,
    }
    res1 = angle(data1)
    print(res1)

    data2 = {
        "decdeg": -90,
    }
    res2 = angle(data2)
    print(res2)

    data3 = {
        "dms": {
            "degrees": -10,
            "minutes": 2,
            "seconds": 3,
        }
    }
    res3 = angle(data3)
    print(res3)


test()

